package cn.stylefeng.guns.modular.tpls.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.tpls.entity.Tpls;
import cn.stylefeng.guns.modular.tpls.mapper.TplsMapper;
import cn.stylefeng.guns.modular.tpls.model.params.TplsParam;
import cn.stylefeng.guns.modular.tpls.model.result.TplsResult;
import  cn.stylefeng.guns.modular.tpls.service.TplsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 模板库 服务实现类
 * </p>
 *
 * @author Dong
 * @since 2023-08-09
 */
@Service
public class TplsServiceImpl extends ServiceImpl<TplsMapper, Tpls> implements TplsService {

    @Override
    public void add(TplsParam param){
        Tpls entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TplsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TplsParam param){
        Tpls oldEntity = getOldEntity(param);
        Tpls newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TplsResult findBySpec(TplsParam param){
        return null;
    }

    @Override
    public List<TplsResult> findListBySpec(TplsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TplsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TplsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Tpls getOldEntity(TplsParam param) {
        return this.getById(getKey(param));
    }

    private Tpls getEntity(TplsParam param) {
        Tpls entity = new Tpls();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<TplsResult> listbyquery(TplsParam param) {
		return this.baseMapper.listbyquery(param);
	}

}
